package com.cuitrip.business.login.model;

import com.alibaba.fastjson.JSONObject;
import com.lab.logtrack.AppLog;
import com.lab.utils.PermissionUtil;

import java.util.HashMap;

/**
 * Created by ak on 15/11/6.
 */
public enum  ThirdAccountParser{

    WECHAT{
        @Override
        public ThirdAccount parseObject(ThirdAccount thirdAccount, HashMap<String, Object> userDataParams) {
            if (userDataParams != null && !userDataParams.isEmpty()){
                JSONObject jsonObject = new JSONObject();
                for (String key : userDataParams.keySet()){
                    jsonObject.put(key, userDataParams.get(key));
                    if (WECHAT_NICKNAME.equals(key)){
                        thirdAccount.setNickName(jsonObject.getString(WECHAT_NICKNAME));
                    }else if (WECHAT_HEAD_IMAGE_URL.equals(key)){
                        thirdAccount.setAvatarPic(jsonObject.getString(WECHAT_HEAD_IMAGE_URL));
                    }
                }
                String thirdObject = jsonObject.toJSONString();
                thirdAccount.setThirdObject(thirdObject);
                AppLog.d("wechat==" + thirdObject);
            }
            return thirdAccount;
        }
    },
    FACEBOOK{


        private String getFacebookProfileUrl(String id){
            return "https://graph.facebook.com/"+id+"/picture?type=large";
        }



        @Override
        public ThirdAccount parseObject(ThirdAccount thirdAccount, HashMap<String, Object> userDataParams) {
            if (userDataParams != null && !userDataParams.isEmpty()){
                JSONObject jsonObject = new JSONObject();
                for (String key : userDataParams.keySet()){
                    jsonObject.put(key, userDataParams.get(key));
                    if (FACEBOOK_NAME.equals(key)){
                        thirdAccount.setNickName(jsonObject.getString(FACEBOOK_NAME));
                    }/*else if (FACEBOOK_PICTURE.equals(key)){
                        Object Picture = userDataParams.get(FACEBOOK_PICTURE);
                        if (Picture instanceof HashMap && ((HashMap)Picture).containsKey(DATA)){
                            Object data = ((HashMap) Picture).get(DATA);
                            if (data instanceof HashMap && ((HashMap)data).containsKey(URL)){
                                String url = (String) ((HashMap) data).get(URL);
                                thirdAccount.setAvatarPic(url);
                            }
                        }
                    }*/else if (PAYMENT_POINTS.equals(key)){
                        // 移除payment points
                        jsonObject.remove(key);
                    }else if (FACEBOOK_EMAIL.equals(key)){
                        thirdAccount.setEmail(jsonObject.getString(FACEBOOK_EMAIL));
                    }else if (FACEBOOK_ID.equals(key)){
                        String facebookId = jsonObject.getString(FACEBOOK_ID);
                        thirdAccount.setAvatarPic(getFacebookProfileUrl(facebookId));
                    }
                }
                String thirdObject = jsonObject.toJSONString();
                thirdAccount.setThirdObject(thirdObject);
                AppLog.d("facebook==" + thirdObject);
            }
            return thirdAccount;
        }

    },
    GOOGLE{
        @Override
        public ThirdAccount parseObject(ThirdAccount thirdAccount, HashMap<String, Object> userDataParams) {
            if (userDataParams != null && !userDataParams.isEmpty()){
                JSONObject jsonObject = new JSONObject();
                for (String key : userDataParams.keySet()){
                    jsonObject.put(key, userDataParams.get(key));
                    if (GOOGLE_NICKNAME.equals(key)){
                        thirdAccount.setNickName(jsonObject.getString(GOOGLE_NICKNAME));
                    }else if (GOOGLE_HEAD_IMAGE_URL.equals(key)){
                        thirdAccount.setAvatarPic(jsonObject.getString(GOOGLE_HEAD_IMAGE_URL));
                    }else if(GOOGLE_EMAIL.equals(key)){
                        thirdAccount.setEmail(jsonObject.getString(GOOGLE_EMAIL));
                    }
                }
                String thirdObject = jsonObject.toJSONString();
                thirdAccount.setThirdObject(thirdObject);
                AppLog.d("google=="+thirdObject);
            }
            return thirdAccount;
        }
    },WEIBO{
        @Override
        public ThirdAccount parseObject(ThirdAccount thirdAccount, HashMap<String, Object> userDataParams) {
            if (userDataParams != null && !userDataParams.isEmpty()){
                JSONObject jsonObject = new JSONObject();
                for (String key : userDataParams.keySet()){
                    jsonObject.put(key, userDataParams.get(key));
                    if (WEIBO_NAME.equals(key)){
                        thirdAccount.setNickName(jsonObject.getString(WEIBO_NAME));
                    }else if (WEIBO_HEAD_IMAGE_URL.equals(key)){
                        thirdAccount.setAvatarPic(jsonObject.getString(WEIBO_HEAD_IMAGE_URL));
                    }
                }
                String thirdObject = jsonObject.toJSONString();
                thirdAccount.setThirdObject(thirdObject);
                AppLog.d("weibo=="+thirdObject);
            }
            return thirdAccount;
        }
    };


    public static final String WEIBO_HEAD_IMAGE_URL = "avatar_large";
    public static final String WEIBO_NAME = "name";

    public static final String WECHAT_NICKNAME = "nickname";
    public static final String WECHAT_HEAD_IMAGE_URL = "headimgurl";

    public static final String FACEBOOK_EMAIL="email";
    public static final String FACEBOOK_NAME = "name";
    public static final String FACEBOOK_PICTURE = "picture";
    public static final String FACEBOOK_ID = "id";
    public static final String DATA = "data";
    public static final String URL = "url";
    public static final String PAYMENT_POINTS = "payment_pricepoints";

    public static final String GOOGLE_NICKNAME = "DisplayName";
    public static final String GOOGLE_HEAD_IMAGE_URL = "image";
    public static final String GOOGLE_EMAIL = "Emails";

    public ThirdAccount parseObject(ThirdAccount thirdAccount, HashMap<String, Object> userDataParams) {
        return new ThirdAccount();
    }




}
